﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Customer;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Contract.Paging;
using Common.Contract.Warranty.Record;
using Common.Contract.Warranty.Request;
using Common.Contract.Warranty.Response;
using Common.Service;
using Common.Service.Command;
using Common.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Warranty.Model.Interface;
using Warranty.Model.Model;

namespace Warranty.ServiceCommand
{
    public class SearchPndHistServiceCommand : ProviderServiceCommand<IProviderFactory, SearchPndHistRequest, SearchPndHistResponse>
    {
        public SearchPndHistServiceCommand(IProviderFactory provider) : base(provider) { }

        protected override void Validate(SearchPndHistRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
        }

        protected override bool OnExecute(SearchPndHistResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            var PndHists = Providers.PndHistory.SearchPndHistory(Request.SerialNo, null, Request.TransferFlag);
            if (PndHists == null || PndHists.Count == 0)
            {
                response.ErrorMessage = string.Format("Can not found PndHistory with Model={0}, SerialNo={1}", Request.Model,Request.SerialNo);
                response.IsSuccess = false;
                return false;
            }

            var records = PndHists.Select(pndHist => new PndHistRecord
            {
                 Moseq = pndHist.Moseq,
                 CmdCode = pndHist.CmdCode,
                 ConfirmDate = pndHist.CfmDate,
                 ConfirmFlag = pndHist.CfmFlag,
                 ConfirmType = pndHist.CfmType,
                 ConfirmUser = pndHist.CfmUser,
                 EndUserCell = pndHist.EndUserCell,
                 MessageBody = pndHist.MoMsgbody,
                 Model = pndHist.Model,
                 PndDays = pndHist.PndDays,
                 PndType = pndHist.PndType,
                 ReceiveDate = pndHist.ReceiveDate,
                 RejectDate = pndHist.RejectDate,
                 RejectFlag = pndHist.RejectFlag.ToBoolean(),
                 RejectUser = pndHist.RejectUser,
                 ResponseMsg = pndHist.RespMsg,
                 ResponseType = pndHist.RespType,
                 SerialNumber = pndHist.SerialNo,
                 ShopCell = pndHist.ShopCell,
                 ShopCode = pndHist.ShopCode,
                 SmsYn = pndHist.SmsYn.ToBoolean(),
                 TransferFlag = pndHist.SoTransferFlag.ToBoolean()
            }).ToList();
            response.Records = records;
            return isSuccess;
        }
    }
}
